package com.z.kq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.z.common.web.vo.request.PageVo;
import com.z.kq.entity.ScheSign;
import com.z.kq.mapper.ScheSignMapper;
import com.z.kq.service.IScheSignService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 班制打卡时间Service业务层处理
 *
 * @author z
 * @date 2023-03-24
 */
@Service
public class ScheSignServiceImpl extends ServiceImpl<ScheSignMapper,ScheSign> implements IScheSignService {

    @Resource
    private ScheSignMapper scheSignMapper;
    /**
     * 查询班制打卡时间
     * @param scheSign 班制打卡时间
     * @param pageVo
     * @return 班制打卡时间 分页集合
     * */
    @Override
    public PageInfo<ScheSign> selectScheSignPage(ScheSign scheSign, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(), pageVo.getLimit());
        List<ScheSign> data = baseMapper.selectScheSignList(scheSign);
        return new PageInfo<>(data);
    }

    @Override
    public List<String> selectScheNames(String scheType, String unitId) {
        List<String> data = scheSignMapper.selectScheNames(scheType, unitId);
        if(data == null || data.size()<1){
            data = scheSignMapper.selectScheNames(scheType, "1");
        }
        return data;
    }

    @Override
    public List<String> selectScheStages(String scheName, String scheType, String unitId) {
        List<String> data = new ArrayList<>();
        List<ScheSign> scheSignList = scheSignMapper.selectList(new QueryWrapper<ScheSign>().eq("sche_Name", scheName).eq("sche_type", scheType).eq("unit_id", unitId).orderByAsc("sche_sort"));
        if(scheSignList == null || scheSignList.size()<1){
            scheSignList = scheSignMapper.selectList(new QueryWrapper<ScheSign>().eq("sche_Name", scheName).eq("sche_type", scheType).eq("unit_id", 1).orderByAsc("sche_sort"));
        }
        Map<String, String> map = new LinkedHashMap<>();
        scheSignList.forEach(scheSign -> {
            if(map.get(scheSign.getScheStage()) == null){
                map.put(scheSign.getScheStage(), "");
            }
            map.put(scheSign.getScheStage(), map.get(scheSign.getScheStage())+ scheSign.getStartTime() + "-" + scheSign.getEndTime() + ",");
        });
        map.forEach((k,v) ->{
            data.add(k + "[" + v.substring(0, v.length() - 1) + "]");
        });
        return data;
    }

}
